﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor
{
    public partial class ArsivEtiketleri : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public bool Stop = false;
        public ArsivEtiketleri()
        {
            InitializeComponent();
        }

        public ArsivEtiketleri(string KstIds, int emptyLabelCount, bool IsCons, bool IsBarkod, string SpName)
            : this()
        {
            EmptyLabelCount = emptyLabelCount;

            ds = Session.DataManager.ExecuteDataSet(SpName, KstIds);


            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r = ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }


            this.DataMember = "Table";
            this.DataSource = ds;

            if (!IsCons)
            {
                trKonstruksiyon.Dispose();
            }

            if (!IsBarkod)
            {
                trBarkod.Dispose();
            }

        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

            EmptyLabelCount--;

            this.DataMember = "Table";
            this.DataSource = ds;

            string strModelDetay = "";
            foreach (DataColumn dc in ds.Tables[0].Columns)
            {
                string activeColName = dc.ColumnName;
                if (GetCurrentColumnValue(activeColName).ToString() != string.Empty)
                {
                    strModelDetay += activeColName + ";";
                }
            }

            string[] Fields = strModelDetay.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < 8; i++)
            {
                ((XRTableCell)this.FindControl("h" + i.ToString(), false)).WidthF = 96.83F;
                if (Fields.Length > i)
                {
                    if (Fields[i] != "Brand")
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = Fields[i];//String.Format("{0,-9}" ,Fields[i])+":";
                    else
                    {
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = "";
                        ((XRTableCell)this.FindControl("h" + i.ToString(), false)).WidthF = 5;
                    }
                    ((XRTableCell)this.FindControl("x" + i.ToString(), false)).DataBindings.Add("Text", null, "Table." + Fields[i]);
                }
                else
                {
                    ((XRTableCell)this.FindControl("h" + i.ToString(), false)).Text = "";
                    ((XRTableCell)this.FindControl("x" + i.ToString(), false)).DataBindings.Add("Text", null, "");
                }

            }
        }


        private void ReSize(object sender)
        {
            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRTableCell label = (XRTableCell)sender;

            float newSize = 6.2f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();
        }



        private void xrTable2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tcKompozisyon_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcKonstruksiyon_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void xrBarCode1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tcKompozisyon_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tbKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            float f = tbKumas.HeightF;
        }

        private void x0_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void x2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void x4_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void x5_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void x6_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

    }
}
